﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace StatesAndEventsTest.source
{
    public class StateMachine
    {
        // Members
        protected BaseState mCurState;
        private StateEngine mStateEngine;

        // Constructor
        public StateMachine()
        {
            mStateEngine = new StateEngine();

            // Install all of our states
            mStateEngine.InstallState(new State_MainMenu());
            mStateEngine.InstallState(new State_Pause());
            // Add new states before here

            mCurState = mStateEngine.GetState("Main Menu");
            Transition(mCurState.Name);
        }

        public void HandleEvent(EventManager.Events eve)
        {
            mCurState.HandleEvent(eve);
        }

        public void Transition(string targetStateName)
        {
            BaseState temp = mCurState;
            // TODO: Change current state
            mCurState.Exit();
            mCurState = mStateEngine.GetState(targetStateName);
            if (mCurState == null)
            {
                Console.WriteLine("StateMachine Error: Could not find target state (" + targetStateName + ")");
                mCurState = temp;
            }
            mCurState.Enter();
        }
        public void Update(GameTime gameTime)
        {
            mCurState.Update(gameTime);
        }
    }
}
